package com.houtai.service.train;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.RetInfo;
import com.houtai.bean.TdTrain;
import com.houtai.bean.TdTrainApply;
import com.houtai.bean.dto.train.TdTrainPage;
import com.houtai.bean.dto.train.apply.TdTrainApplyPage;
import com.houtai.common.util.LogUtils;
import com.houtai.dao.TdTrainApplyDao;
import com.houtai.dao.TdTrainDao;
import com.houtai.dao.TsJzAdminOperationDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2017/7/28.
 */
@Service

public class TrainService {
    private static Logger logger = Logger.getLogger(TrainService.class);
    @Autowired
    private TdTrainDao tdTrainDao;

    @Autowired
    private TdTrainApplyDao tdTrainApplyDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TsJzAdminOperationDao tsJzAdminOperationDao;
    /**
     * @Purpose   查找报名列表
     * @param     page
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo findTdTrainApplyPage(TdTrainApplyPage page, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":findTdTrainApplyPage:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            PageHelper.startPage(page.getCurrent_page(), page.getPage_size(), "apply_id desc");
            List<TdTrainApply> lists = tdTrainApplyDao.findTdTrainApplyList(page);
            PageInfo<TdTrainApply> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("train_id", page.getTrain_id());

            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查找培训报名列表 培训id:" + page.getTrain_id() , request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");

        return retInfo;
    }
    /**
     * @Purpose   查找培训
     * @param     page
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo findTrainPage(TdTrainPage page, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":findTrainPage:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            PageHelper.startPage(page.getCurrent_page(), page.getPage_size(), "sort desc, train_id desc");
            List<TdTrain> lists = tdTrainDao.findTdTrainList(page);
            PageInfo<TdTrain> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("train_cate", page.getTrain_cate());
            map.put("class_way", page.getClass_way());
            map.put("train_title", page.getTrain_title());
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查找培训" , request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");

        return retInfo;
    }
    /**
     * @Purpose   添加培训
     * @param     tdTrain
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo insertTdTrain(TdTrain tdTrain, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":insertTdTrain:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tdTrainDao.insertTdTrain(tdTrain);
            retInfo.setMark("0");
            retInfo.setTip("创建成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"添加培训,id:" + tdTrain.getTrain_id(),request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   修改培训
     * @param     tdTrain
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo updateTdTrain(TdTrain tdTrain, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateTdTrain:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tdTrainDao.updateTdTrain(tdTrain);
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"修改培训,id:" + tdTrain.getTrain_id(),request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   删除培训
     * @param     train_id
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo deleteTdTrain(int train_id, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":deleteTdTrain:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tdTrainDao.updateTdTrainState(train_id);
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"删除培训,id:" + train_id,request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    public TdTrain findTdTrainById(Integer train_id) {
        return tdTrainDao.findTdTrainDetail(train_id);
    }
    public  List<TdTrainApply> findTdTrainApplyExport(TdTrainApplyPage page) {
        return tdTrainApplyDao.findTdTrainApplyList(page);
    }
}
